import scrapy
import re
from exhibitioner.items import ExhibitionerItem

class ExhibitionSpider(scrapy.Spider):
    name = 'exhibition'
    allowed_domains = ['www.91tezhuang.com']
    start_urls = ['http://www.91tezhuang.com/']
    num=1

    def parse(self, response):#进入网站首页
        yield scrapy.Request(self.start_urls[0]+'zhanguanmore.html',callback=self.parse_title)

    def parse_title(self,response):#进入展览馆首页
        st = response.xpath('//*[@id="container"]/div/ul/li')
        for li in st:#获取li列表
            meta={}
            img_url=self.start_urls[0]+li.xpath('.//div[1]//a//img//@src').extract_first()
            fileurl=li.xpath('.//div[1]/a//@href').extract_first()
            srname =li.xpath('.//div[1]//a//img//@alt').extract_first()
            #获取展览过的大型会场
            results = li.xpath('.//div[2]/ul')
            results = results[0].xpath('string(.)').extract()
            results1=re.sub("\n","",''.join(results))
            results2=re.sub("\r","",results1)
            results3=re.sub("\t","",results2)
            yield scrapy.Request(fileurl,callback=self.parese_detail,meta={'img':img_url,'url':fileurl,'name':srname,'results':results3})
        #翻页
        self.num=self.num+1
        if self.num>13:
            return
        yield scrapy.Request(self.start_urls[0]+"zhanguanmore_g"+str(self.num)+".html",callback=self.parse_title)
        
    def parese_detail(self,response):#进入详情页
        item = ExhibitionerItem()
        item['img_url']=response.meta['img']
        item['fileurl']=response.meta['url']
        item['srname']=response.meta['name']
        item['results']=response.meta['results']
        st = response.xpath('//*[@id="container"]/div[2]/div[1]/div[2]')
        contents = st.xpath('string(.)').extract()
        content = ''.join(contents)
        content = re.sub('\n','',content)
        content = re.sub('\r','',content)
        content = re.sub('\t','',content)
        item['content']=content
        yield item